package com.shanghaionstar.carmanage.adapter;

import android.content.Context;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.more.entity.OrderHistoryType;
import com.shanghaionstar.R;

import java.util.ArrayList;
import java.util.List;

public class OrderRecordsAdapter extends CommonAdapter<OrderHistoryType> {
    private Context context;
    private PackageType packageType;

    /**
     * @Description: OrderRecordsAdapter 构造方法
     * @author Xiaofeng Feng
     * @date 2015年9月9日 下午3:21:23
     */
    public OrderRecordsAdapter(Context context, PackageType packageType) {
        super(null, context);
        this.context = context;
        this.packageType = packageType;
    }

    public void addOrders(List<OrderHistoryType> orders){
        if (datas == null)
            datas = new ArrayList<>();
        datas.addAll(orders);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void getView(final int pos, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;
        holder.bindData(datas.get(pos));
    }

    @Override
    public int layoutId() {
        return R.layout.onstar_payme_order_record_item;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById
        private RelativeLayout layout;
        @FindViewById
        private ImageView payme_order_record_flag;
        @FindViewById
        private LinearLayout detail_layout;
        @FindViewById
        private TextView tv_order_record_detail_number, payme_order_record_state, tv_order_record_detail_name, tv_payme_order_record_duration,tv_payme_order_record_date, tv_payme_order_record_car_id, payme_order_record_amount;
    
        public void bindData(OrderHistoryType order){
            tv_order_record_detail_name.setText(order.getPackageInfo().getPackageName());
            payme_order_record_amount.setText("￥" + order.getPackageInfo().getActualPrice() + "");
            String orderDate = order.getPackageInfo().getPurchaseDate();
            if (!CommonUtil.isNull(orderDate)) {
                orderDate = orderDate.substring(0, 10);
                tv_payme_order_record_date.setText(context.getResources().getString(R.string.order_date) + orderDate);
            }
            tv_order_record_detail_number.setText(context.getResources().getString(R.string.order_number)
                    + order.getOrderId());
            String statusName = "";
            if (order.getStatus().equals("1")) {
                statusName = context.getResources().getString(R.string.order_status_1);
            } else if (order.getStatus().equals("2")) {
                statusName = context.getResources().getString(R.string.order_status_2);
            } else if (order.getStatus().equals("3")) {
                statusName = context.getResources().getString(R.string.order_status_3);
            }
            payme_order_record_state.setText(statusName);
            tv_payme_order_record_car_id.setText(context.getString(R.string.order_history_vin)+CommonUtil.maskWithStar(order.getSubscriber().getVin(),3,4));
            if (packageType == PackageType.DATA) {
                tv_payme_order_record_duration.setText(context.getString(R.string.duration)+order.getPackageInfo().getDuration() + context.getString(R.string.ppc_day));
            } else {
                String startDate = order.getPackageInfo().getStartDate();
                String endDate = order.getPackageInfo().getEndDate();
                if (!CommonUtil.isNull(startDate)) {
                    startDate = startDate.substring(0, 10);
                }
                if (!CommonUtil.isNull(endDate)) {
                    endDate = endDate.substring(0, 10);
                }
                tv_payme_order_record_duration.setText(context.getString(R.string.duration)+startDate+ context.getString(R.string.order_start_end_date)+endDate);
            }
        }
    }
}